//
//  CanvasView.m
//  TouchPainter
//
//  Created by Carlo Chung on 9/14/10.
//  Copyright 2010 Carlo Chung. All rights reserved.
//

#import "CanvasView.h"
#import "MarkRenderer.h"

@implementation CanvasView

@synthesize mark=mark_, backgroundImage;


- (id)initWithFrame:(CGRect)frame 
{
  if ((self = [super initWithFrame:frame])) 
  {
    // Initialization code
    [self setBackgroundColor:[UIColor whiteColor]];
    if(nil != self.backgroundImage)
{
    [self drawBackgroundWithImage:self.backgroundImage];
}

  }
  return self;
}
-(void) drawBackgroundWithImage:(UIImage*) image
{
  //  UIColor *background = [[[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"background_texture.png"]]autorelease];
    if( nil != image)
    {
    UIColor *background = [[[UIColor alloc] initWithPatternImage:image]autorelease];
    self.backgroundColor = background;
    }
    else if(nil != self.backgroundImage)
    {
        UIColor *background = [[[UIColor alloc] initWithPatternImage:self.backgroundImage]autorelease];
        self.backgroundColor = background;
    }
    else [self setBackgroundColor:[UIColor whiteColor]];

    
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect 
{
    [self drawBackgroundWithImage:self.backgroundImage];
    //UIGraphicsBeginImageContext(self.frame.size);
  // Drawing code
  CGContextRef context = UIGraphicsGetCurrentContext();
  
  // create a renderer visitor
  MarkRenderer *markRenderer = [[[MarkRenderer alloc] initWithCGContext:context] autorelease];
  
  // pass this renderer along the mark composite structure
  [mark_ acceptMarkVisitor:markRenderer];
   //UIGraphicsEndImageContext();
}


- (void)dealloc 
{
  [mark_ release];
  [super dealloc];
}

@end
